package gui;

import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JList;

import db.GestionBBDD;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * Clase que gestiona el panel de borrado de 
 * una zapatilla.
 * @author Javier
 *
 */
public class EliminarZapatilla extends JPanel {

	/**
	 * Creamos el panel.
	 */
	
	ArrayList<String> vans, nb, nike, asics, otras, lista_ = new ArrayList<String>();
	JList list;
	private void salir(){
		this.setVisible(false);
		MainWindow.frameEliminar.setVisible(false);
		MainWindow.frameEliminar.revalidate();
	}
	
	/**
	 * Devuelve un array de con la descripcion a partir del array de deportivas
	 * 
	 * @param deps Array de deportivas
	 * @return ArrayList de String con la descripcion de cada deportiva
	 */
	private ArrayList<String> devuelveString(ArrayList<Deportiva> deps){
		ArrayList<String> a = new ArrayList<String>();
		for(Deportiva dep : deps){
			a.add(dep.getId()+" - "+dep.getMarca()+" "+dep.getModelo()+", Color: "+dep.getColor());
		}
		return a;
	}
	
	/**
	 * Elimina una zapatilla del catalogo
	 * 
	 * @param vans Panel de deportivas marca Vans
	 * @param nb Panel de deportivas marca New Balance
	 * @param nike Panel de deportivas marca Nike
	 * @param asics Panel de deportivas marca Asics
	 * @param otras Panel de otras marcas
	 */
	public EliminarZapatilla(PanelMarca vans, PanelMarca nb, PanelMarca nike, PanelMarca asics, PanelMarca otras) {
		
		
		this.vans=devuelveString(vans.getDeportivas());
		this.nb=devuelveString(nb.getDeportivas());
		this.nike=devuelveString(nike.getDeportivas());
		this.asics=devuelveString(asics.getDeportivas());
		this.otras=devuelveString(otras.getDeportivas());
		
		JLabel lblSeleccionaLaZapatilla = new JLabel("Selecciona la zapatilla:");

		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				salir();
			}
		});
		
		JButton btnEliminar = new JButton("Eliminar");
		btnEliminar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					String zap = (String)list.getSelectedValue();
					Scanner s = new Scanner(zap);
					int indice = s.nextInt();
					s.close();
					GestionBBDD.eliminarDeportiva(indice);
					//Salir
					salir();
				}catch(Exception es){
					JOptionPane.showMessageDialog(null,
							"No has seleccionado nada!","ERROR",JOptionPane.WARNING_MESSAGE);

				}					
				
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSeleccionaLaZapatilla)
					.addContainerGap(333, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(270, Short.MAX_VALUE)
					.addComponent(btnCancelar)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnEliminar)
					.addContainerGap())
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 425, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(15, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSeleccionaLaZapatilla)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCancelar)
						.addComponent(btnEliminar))
					.addContainerGap())
		);
		
		this.lista_.addAll(this.vans);
		this.lista_.addAll(this.nb);
		this.lista_.addAll(this.nike);
		this.lista_.addAll(this.asics);
		this.lista_.addAll(this.otras);
		list = new JList(this.lista_.toArray());

		scrollPane.setViewportView(list);
		setLayout(groupLayout);

	}

}
